//
//  ForgetPWDViewController.m
//  QRMaster
//
//  Created by myApple on 15/3/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "ForgetPWDViewController.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "MyRsa.h"
#import "Reachability.h"
#import "validateInputValue.h"
#import "ForgetPWDView.h"
#import "AreaChooseController.h"
#import "QRSingleton.h"

@interface ForgetPWDViewController ()<UITextFieldDelegate,ForgetPWDViewDelegate>
{
    ForgetPWDView * forgetView;
}

@end


@implementation ForgetPWDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self prepareTitleView];
    self.title = NSLocalizedString(@"changePassWord", nil);
    [self prepareView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [forgetView.time setFireDate:[NSDate distantPast]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [forgetView.time setFireDate:[NSDate distantFuture]];
    
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float width = ScreenWidth;
    float height = UI_SCREEN_HEIGHT - 64;
    float x = 0;
    float y = 0;
    
    forgetView = [[ForgetPWDView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    forgetView.backgroundColor = [UIColor whiteColor];
    forgetView.delegate = self;
    [self.view addSubview:forgetView];
}


#pragma mark ------------ forgetViewDelegate
-(void)changeArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeInString,NSString * areaOrCountry)
    {
        forgetView.areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeInString];
        forgetView.areaLabel.text = areaOrCountry;
    };
}

#pragma mark ----------- 获取验证码
-(void)getCodeData:(NSMutableDictionary *)postDict
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(FORGETPWD) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
    if ([downLoadData objectForKey:@"data"] != nil) {
            [PromptInfoView showMessage:NSLocalizedString(@"getCheckNumPromt", nil)];
        [self updateTime];
        }
        else if ([[error objectForKey:@"status"]integerValue] == 165) {
            
            [PromptInfoView showMessage:NSLocalizedString(@"accountNoRegist", nil) superView:self.view];
        }
        
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
        }
    }];
}

-(void)updateTime
{
    [forgetView getCodeSuccess];
}

#pragma mark ----------提交忘记密码
-(void)postPassword:(NSMutableDictionary *)postDict
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(RESETPWD) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        if ([downLoadData objectForKey:@"data"] != nil) {
            [PromptInfoView showMessage:NSLocalizedString(@"changePWDSuccess", nil)];
            
            NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
            [userInfo setValue:[postDict objectForKey:@"areaCode"] forKey:@"areaCode"];
            if ([postDict objectForKey:@"mobile"]){
                [userInfo setObject:[postDict objectForKey:@"mobile"] forKey:@"userMobile"];//记住登录号码
            }
            else
            {
                [userInfo setObject:[postDict objectForKey:@"email"] forKey:@"userMobile"];//记住登录邮箱
            }
            [userInfo synchronize];
            [QRSingleton sharedQRSingleton].loginViewShow = YES;
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            if ([[error objectForKey:@"status"]integerValue] == 213)
                
            {
                [PromptInfoView showMessage:NSLocalizedString(@"errorVcode", nil) superView:self.view];
            }
            else
            {
                [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
            }
            
        }
    }
     ];

}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"KCHiddenKeyBoardNotification" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
